package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.entity.dto.MainImageDTO;
import com.sneaker.shower.entity.dto.OfflinePayOrgInfoDTO;
import com.sneaker.shower.entity.dto.OrganizationDTO;
import com.sneaker.shower.service.MainImageService;
import com.sneaker.shower.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api
@RestController
public class OrganizationController {
    private final OrganizationService organizationService;
    private final MainImageService mainImageService;

    @Autowired
    public OrganizationController(OrganizationService organizationService, MainImageService mainImageService) {
        this.organizationService = organizationService;
        this.mainImageService = mainImageService;
    }

    @ApiOperation("添加机构")
    @RequestMapping(value = "/organization/addOrganization", method = RequestMethod.POST)
    public BaseView<Boolean> addOrganization(OrganizationDTO dto) {
        return new BaseView<>(organizationService.addOrganization(dto));
    }

    @ApiOperation("删除机构")
    @RequestMapping(value = "/organization/delOrganization", method = RequestMethod.DELETE)
    public BaseView<Boolean> delOrganization(String guid, String name) {
        return new BaseView<>(organizationService.delOrganization(guid, name));
    }

    @ApiOperation("修改机构")
    @RequestMapping(value = "/organization/modifyOrganization", method = RequestMethod.POST)
    public BaseView<Boolean> modifyOrganization(OrganizationDTO dto) {
        return new BaseView<>(organizationService.modifyOrganization(dto));
    }

    @ApiOperation("批量查询所有机构")
    @RequestMapping(value = "/organization/OrganizationDTO", method = RequestMethod.GET)
    public BaseView<List<OrganizationDTO>> listOrganization() {
        return new BaseView<>(organizationService.listOrganization());
    }

    @ApiOperation("获取与用户距离最短的门店信息，经纬度不传时默认返回成都总店信息")
    @RequestMapping(value = "/organization/currentOrganization", method = RequestMethod.GET)
    public BaseView<OrganizationDTO> currentOrganization(@RequestParam(value = "longitude", required = false) String longitude, @RequestParam(value = "latitude", required = false) String latitude) {
        log.info("获取当前组织信息请求入参：{}", longitude + "," + latitude);
        //30.64242,104.04311
        return new BaseView<>(organizationService.currentOrganization(longitude, latitude));
    }

    @ApiOperation("判断当前门店是否已开启")
    @GetMapping("/organization/check_status")
    public BaseView<Boolean> checkOrgStatus() {
        return new BaseView<>(organizationService.checkStatus());
    }

    @ApiOperation("判断二维码是否是门店支付码")
    @GetMapping("/organization/check_qr_code")
    public BaseView<OfflinePayOrgInfoDTO> checkOrgQrCode(String encryptStr) {
        return new BaseView<>(organizationService.checkQrCode(encryptStr));
    }

    @ApiOperation("上传门店首页图片")
    @PostMapping("/organization/upload_main_images")
    public BaseView<Boolean> uploadMainImage(@RequestBody List<MainImageDTO> mainImageDTOS) {
        return new BaseView<>(mainImageService.saveOrUpdateMainImages(mainImageDTOS));
    }

    @ApiOperation("获取门店首页图片")
    @GetMapping("/organization/get_main_images/{organizationGuid}")
    public BaseView<List<MainImageDTO>> getMainImages(@PathVariable("organizationGuid") String organizationGuid) {
        return new BaseView<>(mainImageService.getOrgImages(organizationGuid));
    }
}
